# Define here the models for your spider middleware
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/spider-middleware.html

from scrapy import signals

# useful for handling different item types with a single interface
from itemadapter import is_item, ItemAdapter


class QfSpiderMiddleware:
    # Not all methods need to be defined. If a method is not defined,
    # scrapy acts as if the spider middleware does not modify the
    # passed objects.

    @classmethod
    def from_crawler(cls, crawler):
        # This method is used by Scrapy to create your spiders.
        s = cls()
        crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
        return s

    def process_spider_input(self, response, spider):
        # Called for each response that goes through the spider
        # middleware and into the spider.

        # Should return None or raise an exception.
        return None

    def process_spider_output(self, response, result, spider):
        # Called with the results returned from the Spider, after
        # it has processed the response.

        # Must return an iterable of Request, or item objects.
        for i in result:
            yield i

    def process_spider_exception(self, response, exception, spider):
        # Called when a spider or process_spider_input() method
        # (from other spider middleware) raises an exception.

        # Should return either None or an iterable of Request or item objects.
        pass

    def process_start_requests(self, start_requests, spider):
        # Called with the start requests of the spider, and works
        # similarly to the process_spider_output() method, except
        # that it doesn’t have a response associated.

        # Must return only requests (not items).
        for r in start_requests:
            yield r

    def spider_opened(self, spider):
        spider.logger.info('Spider opened: %s' % spider.name)


class QfDownloaderMiddleware:
    # Not all methods need to be defined. If a method is not defined,
    # scrapy acts as if the downloader middleware does not modify the
    # passed objects.

    @classmethod
    def from_crawler(cls, crawler):
        # This method is used by Scrapy to create your spiders.
        s = cls()
        crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
        return s

    def process_request(self, request, spider):
        # Called for each request that goes through the downloader
        # middleware.

        # Must either:
        # - return None: continue processing this request
        # - or return a Response object
        # - or return a Request object
        # - or raise IgnoreRequest: process_exception() methods of
        #   installed downloader middleware will be called
        return None

    def process_response(self, request, response, spider):
        # Called with the response returned from the downloader.

        # Must either;
        # - return a Response object
        # - return a Request object
        # - or raise IgnoreRequest
        return response

    def process_exception(self, request, exception, spider):
        # Called when a download handler or a process_request()
        # (from other downloader middleware) raises an exception.

        # Must either:
        # - return None: continue processing this exception
        # - return a Response object: stops process_exception() chain
        # - return a Request object: stops process_exception() chain
        pass

    def spider_opened(self, spider):
        spider.logger.info('Spider opened: %s' % spider.name)


class UserAgentMiddleware(object):
    USER_AGENTS_LIST = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36'

    def process_request(self, request, spider):
        print('-----下载中间件-----')
        request.headers['User-Agent'] = self.USER_AGENTS_LIST
        request.headers['Cookie'] = '_ga=GA1.2.989958779.1605151412; qchatid=10358181-e9d9-4f4f-8de1-412901409918; language=SIMPLIFIED; Hm_lvt_de678bd934b065f76f05705d4e7b662c=1605151412,1606815984; _gid=GA1.2.2056836609.1606815985; sid=5ebd9869-87a1-4eac-9373-842e637e8905; JSESSIONID=aaaFtkwTEFrAM9GMRqxyx; cookieId=8d95345b-8594-40db-aa40-9070f0514708; cookieId=3192e200-8b6a-4f04-83ee-0bcbc8241c51; _ga=GA1.3.989958779.1605151412; _gid=GA1.3.2056836609.1606815985; WINDOW_DEVICE_PIXEL_RATIO=1.25; CITY_NAME=SHANGHAI; Hm_lvt_812c8eced13cc8ce8a86e829b84ce6e2=1606815998; HOUSE_PRICE_TOKEN=95b46deac3c24594b5c2638cfde78823; _jzqc=1; _jzqckmp=1; _jzqa=1.176916744438738530.1605151464.1606818294.1606823477.4; _jzqx=1.1605151464.1606823477.2.jzqsr=shenzhen%2Eqfang%2Ecom|jzqct=/.jzqsr=shanghai%2Eqfang%2Ecom|jzqct=/; Hm_lpvt_812c8eced13cc8ce8a86e829b84ce6e2=1606824247; _qzja=1.1449846016.1606816026532.1606818294225.1606823477178.1606823477178.1606824247047.0.0.0.5.3; _qzjb=1.1606823477178.2.0.0.0; _qzjc=1; _qzjto=5.3.0; _jzqb=1.2.10.1606823477.1; Hm_lpvt_de678bd934b065f76f05705d4e7b662c=1606824247'

